export default {
    mounted(el, binding, vnode) {
        nextTick(() => {
            const dialogHeaderEl = el.querySelector(".el-dialog__header")
            const dragDom = el.querySelector(".el-dialog")
            dialogHeaderEl.style.cursor = "move";
            dragDom.style.top = "18%";
            dragDom.style.left = "1%";

            // 弹框可拉伸最小宽高
            let minWidth = 0;
            let minHeight = 0;

            setTimeout(() => {
                minWidth = dragDom.clientWidth;
                minHeight = dragDom.clientHeight;
            }, 300);

            // 获取原有属性，兼容 IE 和现代浏览器
            const getStyle = (dom, attr) => {
                if (window.getComputedStyle) {
                    return window.getComputedStyle(dom, null).getPropertyValue(attr);
                } else {
                    return "";
                }
            };

            dialogHeaderEl.onmousedown = (e) => {
                e.preventDefault();
                // 鼠标按下，计算当前元素距离可视区的距离
                const disX = e.clientX - dialogHeaderEl.offsetLeft;
                const disY = e.clientY - dialogHeaderEl.offsetTop;

                const dragDomWidth = dragDom.offsetWidth;
                const dragDomHeight = dragDom.offsetHeight;

                const screenWidth = document.body.clientWidth;
                const screenHeight = document.body.clientHeight;

                const minDragDomLeft = dragDom.offsetLeft;
                const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;

                const minDragDomTop = dragDom.offsetTop;
                const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomHeight;

                // 获取到的值带px 正则匹配替换
                let styL = getStyle(dragDom, "left");
                let styT = getStyle(dragDom, "top");

                if (typeof styL === "string" && styL.includes("%")) {
                    styL = (parseFloat(styL) / 100) * document.body.clientWidth;
                    styT = (parseFloat(styT) / 100) * document.body.clientHeight;
                } else {
                    styL = parseFloat(styL);
                }

                document.onmousemove = function (e) {
                    // 通过事件委托，计算移动的距离
                    let left = e.clientX - disX;
                    let top = e.clientY - disY;

                    // 边界处理
                    if (-left > minDragDomLeft) {
                        left = -minDragDomLeft;
                    } else if (left > maxDragDomLeft) {
                        left = maxDragDomLeft;
                    }

                    if (-top > minDragDomTop) {
                        top = -minDragDomTop;
                    } else if (top > maxDragDomTop) {
                        top = maxDragDomTop;
                    }

                    // 移动当前元素
                    dragDom.style.left = `${left + styL}px`;
                    dragDom.style.top = `${top + styT}px`;

                    // emit onDrag event
                    vnode.component?.emit("dragDialog");
                };

                document.onmouseup = function () {
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
            };

            // 双击弹窗顶部 最大化 / 最小化
            dialogHeaderEl.ondblclick = () => {
                if (vnode.context && typeof vnode.context.setFullScreen === "function") {
                    vnode.context.setFullScreen();
                } else {
                    console.error("setFullScreen 方法未定义或 vnode.context 不存在");
                }
            };

            // 缩放窗口
            const resizeEl = document.createElement("div");
            dragDom.appendChild(resizeEl);
            // 在弹窗右下角加上一个10-10px的控制块
            resizeEl.style.cursor = "nwse-resize";
            resizeEl.style.position = "absolute";
            resizeEl.style.height = "10px";
            resizeEl.style.width = "10px";
            resizeEl.style.right = "0px";
            resizeEl.style.bottom = "0px";

            // 鼠标拉伸弹窗
            resizeEl.onmousedown = (e) => {
                // 鼠标按下，计算当前元素距离可视区的距离
                const disX = e.clientX - resizeEl.offsetLeft;
                const disY = e.clientY - resizeEl.offsetTop;

                document.onmousemove = function (e) {
                    e.preventDefault(); // 移动时禁用默认事件
                    // 通过事件委托，计算移动的距离
                    const x = e.clientX - disX + 10;
                    const y = e.clientY - disY + 10;
                    // 比较是否小于最小宽高
                    dragDom.style.width = x > minWidth ? `${x}px` : `${minWidth}px`;
                    dragDom.style.height = y > minHeight ? `${y}px` : `${minHeight}px`;
                };

                document.onmouseup = function () {
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
            };
        });
    }
};
